<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Transaksi extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');

		$this->load->library('grocery_CRUD');
	}

	public function _example_output($output = null)
	{
		$this->load->view('master/grid',$output);
	}
	public function detail($header_id)
	{
		
		$this->load->vars('title','Detail Transaksi');

		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		//$this->config->set_item('grocery_crud_dialog_forms',false);
		$crud->where( empty($header_id) ? 'header_id IS NULL' : array('header_id' => $header_id) );
		$crud->set_table('detail');

		$crud->columns('seq_no','HS','kategori','Deskripsi_Barang','Jumlah','Satuan');
		$crud->display_as('HS','No HS');
		$crud->display_as('seq_no','No');
		//$crud->set_relation('header_id','header','Nomor_COI');
		$crud->unset_export();
		$crud->unset_print();
		$crud->set_subject('Detail Transaksi');
		$db = $this->db->get('satuan')->result();
		$daftar_satuan = array();
		foreach ($db as $item ) {
			$daftar_satuan[$item->kode] = $item->kode . ' - ' . $item->uraian; 
		}
		$crud->field_type('Satuan','dropdown',$daftar_satuan);

		$state = $crud->getState();

		if( $state == 'list' )
		{
			//$ci =& get_instance();
			//$ci->view->_view_path = FCPATH . '/assets/templates/view/';
			$detail = $this->db->where('id',$header_id)->get('header')->row();

			$data = $this->load->view('_detail_transaksi',array('detail'=>$detail),TRUE);
			$this->load->vars('pre_content',$data);
		}
		//print_r($state);
		$crud->field_type('header_id','hidden',$header_id);

		//$crud->add_fields('header_id','seq_no','kategori','HS','Deskripsi_Barang','Jumlah','Satuan','nilai','nilai_usd',);
		$crud->display_as('pwr','Power Weight Ratio');
		$crud->display_as('hp_ps','HP/PS');
		$crud->display_as('nilai','Nilai USD');
		$crud->display_as('nilai_usd','Nilai USD');
		$crud->display_as('satuan2','Satuan');
		$output = $crud->render();

		
		$this->_example_output($output);
	}
	public function header()
	{
		
		$this->load->vars('title','Data Transaksi');

		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		//$this->config->set_item('grocery_crud_dialog_forms',false);
		$crud->set_table('header');
		$crud->set_subject('Transaksi');
		$crud->unset_read();
		// 
		$crud->columns('Nomor_COI','Tanggal_COI','Kirim','Mata_Uang','Kode_Cabang','Nomor_Izin_Impor','Tanggal_Izin','Tanggal_Izin_Impor_Exp','Nomor_API','NPWP');
		// 

		///
		$crud->add_fields("Kirim","Nomor_COI", "Tanggal_COI", "Kode_Cabang", "Nomor_Izin_Impor", "Tanggal_Izin", "Tanggal_Izin_Impor_Exp", "Importir", "NPWP", "Nomor_API", "Tanggal_API", "Alamat_Importir", "Telp_Importir", "Fax_Importir", "Eksportir", "Alamat_Eksportir", "Telp_Eksportir", "Fax_Eksportir","Kode_Negara_Muat","Kode_Pelabuhan_Tujuan","Mata_Uang", "FOB", "Alamat_Pemeriksaan", "Tanggal_Periksa", "Biaya_Periksa", "fileXLS", "Rate","id_afiliasi", "cert_no", "ref_invoice_no", "ref_invoice_date", "ref_packing_no", "ref_packing_date", "ref_awb_no", "ref_awb_date", "inspected_goods", "coi_date", "coi_ttd", "roi_no", "roi_date", "roi_ref1", "roi_ref2", "roi_ref3", "roi_result1", "roi_result2", "roi_result3", "roi_inspector", "io_no", "ql_no");
		$crud->edit_fields("Nomor_COI", "Tanggal_COI", "Kode_Cabang", "Nomor_Izin_Impor", "Tanggal_Izin", "Tanggal_Izin_Impor_Exp", "Importir", "NPWP", "Nomor_API", "Tanggal_API", "Alamat_Importir", "Telp_Importir", "Fax_Importir", "Eksportir", "Alamat_Eksportir", "Telp_Eksportir", "Fax_Eksportir","Kode_Negara_Muat","Kode_Pelabuhan_Tujuan","Mata_Uang", "FOB", "Alamat_Pemeriksaan", "Tanggal_Periksa", "Biaya_Periksa", "fileXLS", "Rate","id_afiliasi", "cert_no", "ref_invoice_no", "ref_invoice_date", "ref_packing_no", "ref_packing_date", "ref_awb_no", "ref_awb_date", "inspected_goods", "coi_date", "coi_ttd", "roi_no", "roi_date", "roi_ref1", "roi_ref2", "roi_ref3", "roi_result1", "roi_result2", "roi_result3", "roi_inspector", "io_no", "ql_no");
		///
		//$crud->field_type('Kirim','hidden','Belum');

		$db = $this->db->get('mata_uang')->result();

		$daftar_mata_uang = array();
		foreach ($db as $item ) {
			$daftar_mata_uang[$item->kode] = $item->kode; 
		}

		$crud->field_type('Mata_Uang','dropdown',$daftar_mata_uang);

		$db = $this->db->get('negara')->result();
		$daftar_negara = array();
		foreach ($db as $item ) {
			$daftar_negara[$item->kode] = $item->kode . '-' . $item->uraian; 
		}

		$crud->field_type('Mata_Uang','dropdown',$daftar_mata_uang);

		$db = $this->db->get('negara')->result();
		$daftar_negara = array();
		foreach ($db as $item ) {
			$daftar_negara[$item->kode] = $item->kode . '-' . $item->uraian; 
		}

		$crud->field_type('Kode_Negara_Muat','dropdown',$daftar_negara);


		$db = $this->db->get('pelabuhan')->result();
		$daftar_pelabuhan = array();
		foreach ($db as $item ) {
			$daftar_pelabuhan[$item->kode] = $item->kode . '-' . $item->uraian; 
		}

		$crud->field_type('Kode_Pelabuhan_Tujuan','dropdown',$daftar_pelabuhan);

		$crud->field_type('Kirim','hidden');

		$crud->display_as('FOB','FOB Barang');
		$crud->display_as('Rate','Rate ke USD');
		$crud->display_as('fileXLS','Detail File');
		$crud->display_as('cert_no','No Sertifikat');
		$crud->display_as('coi_ttd','Tertanda');

		$crud->set_field_upload('fileXLS','assets/uploads/files');
		$crud->order_by('id','desc');
		$crud->unset_export();
		$crud->unset_print();
		$crud->add_action('', 'detail', 'transaksi/detail','ui-icon ui-icon-cart transaksi_detail_cmd');
		$crud->add_action('', 'kirim','transaksi/kirim','ui-icon ui-icon-arrowstop-1-e transaksi_kirim_cmd');
		$crud->add_action('coi',base_url().'assets/images/c.png', 'transaksi/coi','');
		$crud->add_action('roi',base_url().'assets/images/r.png', 'transaksi/roi','');

		$crud->set_relation('id_afiliasi','cabang','nama');
		$crud->display_as('id_afiliasi','Cabang Afiliasi');

		//$output->title = ;
		$output = $crud->render();

		$this->_example_output($output);
	}

	public function coi($id)
	{
		$data['tr'] 	= $this->db->select('t.*,n.uraian as Kode_Negara_Muat,p.uraian as Kode_Pelabuhan_Tujuan')
								   ->where('t.id',$id)
								   ->from('header t')
								   ->join('negara n','n.kode = t.Kode_Negara_Muat')
								   ->join('pelabuhan p','p.kode = t.Kode_Pelabuhan_Tujuan')
								   ->get()
								   ->row();
		$data['detail'] = $this->db->where('header_id',$id)->get('detail')->result_object();

		$html = $this->load->view('pdfs/_coi', $data, true); // render the view into HTML
			     
	     $this->load->library('pdf');
	     $pdf = $this->pdf->load();
	  

		 $pdf->WriteHTML($html);
		 $pdf->SetFooter('Page {PAGENO} of {nbpg}');
		 $pdf->AddPage('L');

		 
		 $html = $this->load->view('pdfs/_inspected_goods', $data, true); 
		 $pdf->WriteHTML($html);
		 $pdf->Output('coi-'.$id.'.pdf','D');  
	}
	public function roi($id)
	{
		$data['tr'] 	= $this->db->select('t.*,n.uraian as Kode_Negara_Muat,p.uraian as Kode_Pelabuhan_Tujuan')
								   ->where('t.id',$id)
								   ->from('header t')
								   ->join('negara n','n.kode = t.Kode_Negara_Muat')
								   ->join('pelabuhan p','p.kode = t.Kode_Pelabuhan_Tujuan')
								   ->get()
								   ->row();
		$data['detail'] = $this->db->where('header_id',$id)->get('detail')->result_object();
 $this->load->library('pdf');
	     $pdf = $this->pdf->load();
		$html = $this->load->view('pdfs/_roi', $data, true); // render the view into HTML
		$html2 = $this->load->view('pdfs/_inspected_goods_roi', $data, true); 
		
		if($this->input->get('output') != 'html')
		{	     
	    
	  

		$pdf->WriteHTML($html);
		 $pdf->SetFooter('Page {PAGENO} of {nbpg}');
		 $pdf->AddPage('L');

		 
		 
		 $pdf->WriteHTML($html2);
		 $pdf->Output('roi-'.$id.'.pdf','D'); 
		}else{
			echo($html2);
		}
	}
	public function kirim($id)
	{
		if(!empty($id))
		{
			$transaksi = $this->db->where('id',$id)->get('header')->row();
			if($transaksi)
			{
				if($transaksi->Kirim == 'Sudah')
				{
					echo json_encode(array('status'=>'sudah_dikirim_sebelumnya'));
				}
				else
				{
					$this->db->where('id',$id)->update('header',array('Kirim'=>'Sudah'));
					echo json_encode(array('status'=>'sudah_dikirim'));
				}
			}
			else
			{
				echo json_encode(array('status'=>'id_tidak_terdaftar'));
			}
		}
		else
		{
			echo json_encode(array('status'=>'id_kosong'));
		}
	}
}